<?php
class Home_model extends CI_Model {
	
	function Home_model() {
		parent::__construct();	
		$this->load->database();
	}
	
	function loadCategory() {
		$query = $this->db->get ( 'category' );
		return $query->result_array ();
	}
	
	function loadSlide(){
		$query = $this->db->get ( 'slideshow' );
		return $query->result_array ();
	}
	
	function loadNews(){
		$query = $this->db->get ( 'news' );
		return $query->result_array ();
	}
	
	function loadProvidersByCate($cateId){
		
		$sql = 'SELECT provider.id,provider.name,logo

			FROM provider_category JOIN provider ON provider.id = provider_id

			JOIN category ON category_id = category.id
			
			WHERE category.id = '.$cateId;
		
		$query = $this->db->query($sql);
		return $query->result_array();
//		$this->db->where('category_id', $cateId);
//		$query = $this->db->get('provider_category');
//		if($query->num_rows() >= 1) {
//			$result = $query->result_array();
//			$providers = array();
//			$index = 0;
//			foreach ($result as $item) {
////echo $index.'-'.$item['provider_id'].'<br/>';
//				$this->db->where('id',$item['provider_id']);
//				$query = $this->db->get ( 'provider' );
//				$providers[$index] = $query->row_array();
//				$index++;
//			}
//			return $providers;
//		}else {
//			return NULL;
//		}
	}
	
	function loadProvidersByPrice($priceId){
//		$this->db->where('pricePolicy_id', $priceId);
//		$query = $this->db->get('provider_pricepolicy');
//		if($query->num_rows() >= 1) {
//			$result = $query->result_array();
//			$providers = array();
//			$index = 0;
//			foreach ($result as $item) {
//				$providers[$index] = $this->db->get ( 'provider', array('id' => $item['provider_id']) )->row_array();
//				$index++;
//			}
//			return $providers;
//		}else {
//			return NULL;
//		}
$sql = 'SELECT provider.id,provider.name,logo

			FROM provider_pricepolicy JOIN provider ON provider.id = provider_id

			JOIN pricepolicy ON pricePolicy_id = pricepolicy.id
			
			WHERE pricepolicy.id = '.$priceId;
		
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	function loadProvidersByDistrict($district){
		$district = urldecode ( $district );
		$this->db->like('district', $district);
		$query = $this->db->get('provider');
		if($query->num_rows() >= 1) {
			$result = $query->result_array();
			return $result;
		}else {
			return NULL;
		}
	}
	
	function loadNewsById($newsId){
		$this->db->where('id', $newsId);
		$query = $this->db->get('news');
		if($query->num_rows() >= 1) {
			$result = $query->row_array();
			return $result;
		}else {
			return NULL;
		}
	}
}